<?php

namespace common\models;

use builder\behaviors\DatetimeBehavior;
use Yii;

/**
 * This is the model class for table "{{%app_logs}}".
 *
 * @property int $id
 * @property string $level 日志等级, DEBUG、INFO、WARNING、ERROR
 * @property string $category 种类, 例: 订单支付
 * @property string|null $prefix 客户端
 * @property string|null $message 信息
 * @property string $created_at 记录时间
 */
class AppLogs extends \builder\base\BaseActiveRecord
{
    /**
     * 重构日期行为配置
     * @return array
     */
    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['timestampBehavior'] = [
            'class' => DatetimeBehavior::class,
            'attributes' => [
                self::EVENT_BEFORE_INSERT => ['created_at'],
            ],
        ];

        return $behaviors;
    }

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%app_logs}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['level', 'category', 'prefix', 'message'], 'string'],
            [['created_at'], 'safe'],
            [['level'], 'string', 'max' => 20],
            [['category'], 'string', 'max' => 255],
        ];
    }

    /**
     * 场景
     * @return array
     */
    public function scenarios()
    {
        $scenarios = parent::scenarios();
        //新增
        $scenarios['log_create'] = ['level', 'category', 'prefix', 'message'];

        return $scenarios;
    }

    /**
     * @return string[]
     */
    public static function levelLabels()
    {
        //level 日志等级, DEBUG、INFO、WARNING、ERROR
        return [
            'DEBUG' => '调试',
            'INFO' => '信息',
            'WARNING' => '警告',
            'ERROR' => '错误',
        ];
    }

    /**
     * @param string $level 日志等级, DEBUG、INFO、WARNING、ERROR
     * @return string
     */
    public static function levelLabel($level)
    {
        return @self::levelLabels()[$level] ?: '';
    }

    /**
     * @param string $level 日志等级, DEBUG、INFO、WARNING、ERROR
     * @return string
     */
    public static function levelLabelColor($level)
    {
        $colors = [
            'DEBUG' => 'gray1',
            'INFO' => 'success',
            'WARNING' => 'warning',
            'ERROR' => 'red',
        ];
        return @$colors[$level] ?: '';
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'level' => '日志等级',
            'category' => '种类',
            'prefix' => '客户端',
            'message' => '信息',
            'created_at' => '记录时间',
        ];
    }
}
